<?php

    require 'DBControl.php';
    require 'facebook/facebook.php';
    
//    header('P3P:CP="IDC DSP COR ADM DEVi TAIi PSA PSD IVAi IVDi CONi HIS OUR IND CNT"'); 
//    session_start();
    
    $mapping = array(
    'book' => 8,
    'author' => 8,
    'publisher' => 8,
    'book shop' => 8,
    'library' => 8,
    'magazine' => 8,
    'editor' => 8,
    'musical instrument' => 6,
    'cars' => 13,
    'bags/luggage' => 11,
    'camera/photo' => 0,
    'clothing' => 16,
    'computers' => 9,
    'software' => 9,
    'electronics' => 2,
    'health/beauty' => 10,
    'appliances' => 12,
    'building materials' => 15,
    'home decor' => 12,
    'furniture' => 12,
    'household supplies' => 12,
    'kitchen/cooking' => 12,
    'patio/garden' => 12,
    'tools/equipment' => 15,
    'wine/spirits' => 14,
    'jewellery/watches' => 16,
    'films/music' => 6,
    'movies/music' => 6,
    'movie general' => 4,
    'pet supplies' => 12,
    'outdoor gear/sporting goods' => 7,
    'baby goods/kids goods' => 12,
    'food/beverages' => 14,
    'drugs' => 10,
    'vitamins/supplements' => 10,
    'games/toys' => 17,
    'aerospace/defence' => 18,
    'automobiles and parts' => 13,
    'bank/financial institution' => 19,
    'biotechnology' => 18,
    'computers/technology' => 9,
    'consulting/business services' => 1,
    'education' => 3,
    'energy/utility' => 2,
    'engineering/construction' => 18,
    'food/beverages' => 14,
    'government organization' => 19,
    'health/beauty' => 10,
    'health/medical/pharmaceuticals' => 10,
    'internet/software' => 9,
    'legal/law' => 19,
    'media/news/publishing' => 8,
    'non-governmental organization (ngo)' => 19,
    'profit organization' => 19,
    'organization' => 19,
    'political organization' => 19,
    'political party' => 19,
    'actor/director' => 4,
    'movie' => 4,
    'producer' => 4,
    'writer' => 4,
    'studio' => 4,
    'cinema' => 4,
    'tv/film award' => 4,
    'fictional character' => 4,
    'cinema' => 4,
    'concert venue' => 6,
    'book shop' => 8,
    'library' => 8,
    'restaurant/cafe' => 14,
    'bar' => 20,
    'club' => 20,
    'outdoor gear/sporting goods' => 7,
    'education' => 3,
    'hotel' => 11,
    'landmark' => 11,
    'transit stop' => 11,
    'airport' => 11,
    'sports venue' => 7,
    'arts/entertainment/nightlife' => 20,
    'automotive' => 13,
    'spas/beauty/personal care' => 5,
    'event planning/event services' => 5,
    'bank/financial services' => 19,
    'food/grocery' => 14,
    'health/medical/pharmacy' => 10,
    'home improvement' => 15,
    'business services' => 1,
    'community/government' => 19,
    'attractions/things to do' => 5,
    'sports/recreation/activities' => 7,
    'tours/sightseeing' => 11,
    'transport' => 11,
    'hospital/clinic' => 10,
    'museum/art gallery' => 0,
    'school' => 3,
    'university' => 3,
    'album' => 6,
    'song' => 6,
    'musician/band' => 6,
    'musical instrument' => 6,
    'playlist' => 6,
    'music video' => 6,
    'concert tour' => 6,
    'concert venue' => 6,
    'radio station' => 6,
    'record label' => 6,
    'music award' => 6,
    'music chart' => 6,
    'actor/director' => 4,
    'producer' => 4,
    'writer' => 4,
    'fictional character' => 4,
    'musician/band' => 6,
    'author' => 8,
    'editor' => 8,
    'athlete' => 7,
    'artist' => 0,
    'journalist' => 8,
    'news personality' => 5,
    'chef' => 14,
    'doctor' => 10,
    'business person' => 1,
    'comedian' => 5,
    'entertainer' => 5,
    'monarch' => 19,
    'teacher' => 3,
    'dancer' => 0,
    'politician' => 19,
    'government official' => 19,
    'coach' => 7,
    'athlete' => 7,
    'sports league' => 7,
    'professional sports team' => 7,
    'coach' => 7,
    'amateur sports team' => 7,
    'school sports team' => 7,
    'sports venue' => 7,
    'actor/director' => 4,
    'writer' => 4,
    'studio' => 4,
    'tv/film award' => 4,
    'fictional character' => 4,
    'tv show' => 4,
    'tv channel' => 4,
    'tv channel' => 4,
    'episode' => 4,
    'arts/humanities website' => 0,
    'business/economy website' => 1,
    'computers/internet website' => 9,
    'education website' => 3,
    'entertainment website' => 5,
    'government website' => 19,
    'health/wellness website' => 10,
    'home/garden website' => 12,
    'recreation/sports website' => 7,
    'regional website' => 11,
    'science website' => 18,
    'society/culture website' => 0,
    'local/travel website' => 11,
    'travel/leisure' => 11,
    'telecommunication' => 2
    );
    
    $weight_likes_query = "SELECT * FROM 
            (select i.id, title, mediumimagelink, amazonurl, 
            (
                (il.weights[1] * ?) + 
                (il.weights[2] * ?) + 
                (il.weights[3] * ?) + 
                (il.weights[4] * ?) + 
                (il.weights[5] * ?) + 
                (il.weights[6] * ?) + 
                (il.weights[7] * ?) + 
                (il.weights[8] * ?) + 
                (il.weights[9] * ?) + 
                (il.weights[10] * ?) + 
                (il.weights[11] * ?) + 
                (il.weights[12] * ?) + 
                (il.weights[13] * ?) + 
                (il.weights[14] * ?) + 
                (il.weights[15] * ?) + 
                (il.weights[16] * ?) + 
                (il.weights[17] * ?) + 
                (il.weights[18] * ?) + 
                (il.weights[19] * ?) + 
                (il.weights[20] * ?) + 
                (il.weights[21] * ?)
            ) as calculated 
            FROM item i 
            JOIN itemslikesvect il ON i.id = il.item_id) a
            ORDER BY calculated DESC LIMIT ?";
    
        $weight_likes_query_for_santa = "SELECT * FROM 
            (select i.id, title, mediumimagelink, amazonurl, 
            (
                (il.weights[1] * ?) + 
                (il.weights[2] * ?) + 
                (il.weights[3] * ?) + 
                (il.weights[4] * ?) + 
                (il.weights[5] * ?) + 
                (il.weights[6] * ?) + 
                (il.weights[7] * ?) + 
                (il.weights[8] * ?) + 
                (il.weights[9] * ?) + 
                (il.weights[10] * ?) + 
                (il.weights[11] * ?) + 
                (il.weights[12] * ?) + 
                (il.weights[13] * ?) + 
                (il.weights[14] * ?) + 
                (il.weights[15] * ?) + 
                (il.weights[16] * ?) + 
                (il.weights[17] * ?) + 
                (il.weights[18] * ?) + 
                (il.weights[19] * ?) + 
                (il.weights[20] * ?) + 
                (il.weights[21] * ?)
            ) as calculated 
            FROM item i 
            JOIN itemslikesvect il ON i.id = il.item_id
            WHERE i.id NOT IN (SELECT item_id from userschoice WHERE users_id=?)) a
            ORDER BY calculated DESC LIMIT ? OFFSET ?;";

    function array_display($arr)
    {
        echo '<pre>';
        print_r($arr);
        echo '</pre>';
    }

    function parseDate($date)
    {
        $elements = split('/',$date);
        return mktime(0,0,0,$elements[0],$elements[1],$elements[2]);
    }

    function getAge($date)
    {
        return date("Y",(time()-parseDate($date)))-1970;
    }

    function getWeightsVector($likes, $mapping)
    {
//        $mapping = file_get_contents('categoriesMap');
//        $mapping = unserialize($mapping);
        $user_weights = array_fill(0, VECTOR_LENGTH, 0);
        $sum = 0;
        foreach ($likes as $page)
        {
            $category = strtolower($page['category']);
            if (array_key_exists($category, $mapping))
            {
                $user_weights[$mapping[$category]] += 1;
                $sum += 1;
            }
            else
            {
                //eventually add dumping non-mapped categories
            }
        }

        foreach ($user_weights as &$value) 
        {
            $value = $value/$sum;
        }

        return $user_weights;
    }
    
    function weightsArrayToString($arr)
    {
        $out = '{';
        foreach ($arr as $elem)
        {
            $out = $out.$elem.',';
        }
        $out = substr_replace($out,"",-1);
        $out = $out.'}';
        return $out;
    }
    
    function weightsStringToArray($string)
    {
        $out = substr($string, 1, strlen($string)-2);
        $out = explode(',', $out);
        foreach ($out as &$elem)
        {
            $elem = (float)$elem;
        }
        return $out;
    }
?>
